﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Light.Utility.WinUI.Controls
{
    /// <summary>
    /// 支持鼠标移动，改变大小
    /// </summary>
    public partial class LabelExt : Label
    {
        public LabelExt()
        {
            InitializeComponent();
            this.BackColor = Color.Black;
            this.BorderStyle = BorderStyle.FixedSingle;
        }


        public LabelExt(IContainer container)
            : this()
        {
            container.Add(this);
        }

        private int unit = 1;
        private bool moving = false;
        private Size size = new Size(0, 0);
        private Point offset = new Point(0, 0);
        private int left, top, width, height = 0;
        private EnumAdjust adjust = EnumAdjust.Nothing;



        private Rectangle RecAdjustSize
        {
            get { return new Rectangle(this.Width - 15, this.Height - 15, 15, 15); }
        }
        private Rectangle RecAdjustPostion
        {
            get { return new Rectangle(0, 0, this.Width, this.Height); }
        }


        #region 鼠标事件
        protected override void OnMouseDown(MouseEventArgs e)
        {
            moving = true;
            offset = new Point(e.X, e.Y);
            size.Width = this.Width;
            size.Height = this.Height;

            if (RecAdjustSize.Contains(e.X, e.Y)) //调整大小
            {
                this.Cursor = Cursors.SizeNWSE;
                adjust = EnumAdjust.Size;
            }
            else if (RecAdjustPostion.Contains(e.X, e.Y)) //调整位置
            {
                this.Cursor = Cursors.SizeAll;
                adjust = EnumAdjust.Postion;
            }
            else
            {
                adjust = EnumAdjust.Nothing;
            }
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            Cursor = Cursors.Default;
            moving = false;

            base.OnMouseUp(mevent);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            SetCursor(e.X, e.Y);
            left = this.Left;
            top = this.Top;
            width = this.Width;
            height = this.Height;
            if (moving)
            {
                switch (adjust)
                {
                    case EnumAdjust.Size:
                        width = size.Width + (e.X - offset.X);
                        height = size.Height + (e.Y - offset.Y);
                        break;
                    case EnumAdjust.Postion:
                        left = Location.X + (e.X - offset.X);
                        top = Location.Y + (e.Y - offset.Y);
                        break;
                }
                this.SetBounds(left, top, width, height);
            }
            base.OnMouseMove(e);
        }
        #endregion

        #region 键盘事件


        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            if (e.Alt) //调整大小
            {
                switch (e.KeyCode)
                {
                    case Keys.Left:
                        this.Width -= unit;
                        break;
                    case Keys.Right:
                        this.Width += unit;
                        break;
                    case Keys.Down:
                        this.Height += unit;
                        break;
                    case Keys.Up:
                        this.Height -= unit;
                        break;
                }
                return;
            }
            if (e.Control)
            {
                switch (e.KeyCode) //调整位置
                {
                    case Keys.Left:
                        this.Left -= unit;
                        break;
                    case Keys.Right:
                        this.Left += unit;
                        break;
                    case Keys.Up:
                        this.Top -= unit;
                        break;
                    case Keys.Down:
                        this.Top += unit;
                        break;
                }
            }
        }

        #endregion


        /// <summary>
        /// 设置光标样式
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private void SetCursor(int x, int y)
        {
            if (!moving && RecAdjustSize.Contains(x, y))
            {
                this.Cursor = Cursors.SizeNWSE;
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }
    }

}
